
import java.util.ArrayList;

import types.SortType;
import types.JavaType;
import data.cache.table.CacheTable;
import data.cache.table.CacheTableField;
import data.cache.table.CacheTableRow;

public class TestCacheTable {
	public static void main(String[] args)
	{
		CacheTable cacheTable=new CacheTable("test");
		CacheTableRow cacheTableRow=new CacheTableRow();
		cacheTableRow.addField("id", new CacheTableField(1, JavaType.INTEGER));
		cacheTableRow.addField("userName", new CacheTableField("test1", JavaType.STRING));
		cacheTableRow.addField("number", new CacheTableField(100, JavaType.INTEGER));
		cacheTable.addRow(new String[]{"1"}, cacheTableRow);
		
		cacheTableRow=new CacheTableRow();
		cacheTableRow.addField("id", new CacheTableField(2, JavaType.INTEGER));
		cacheTableRow.addField("userName", new CacheTableField("test2", JavaType.STRING));
		cacheTableRow.addField("number", new CacheTableField(300, JavaType.INTEGER));
		cacheTable.addRow(new String[]{"2"}, cacheTableRow);
		
		cacheTableRow=new CacheTableRow();
		cacheTableRow.addField("id", new CacheTableField(3, JavaType.INTEGER));
		cacheTableRow.addField("userName", new CacheTableField("test3", JavaType.STRING));
		cacheTableRow.addField("number", new CacheTableField(300, JavaType.INTEGER));
		cacheTable.addRow(new String[]{"3"}, cacheTableRow);
		
		cacheTableRow=new CacheTableRow();
		cacheTableRow.addField("id", new CacheTableField(3, JavaType.INTEGER));
		cacheTableRow.addField("userName", new CacheTableField("test4", JavaType.STRING));
		cacheTableRow.addField("number", new CacheTableField(100, JavaType.INTEGER));
		cacheTable.addRow(new String[]{"4"}, cacheTableRow);
		
//		cacheTable.getRow("1")[];
		
//		System.out.println(cacheTable.getRow("1").getFieldData("id").getData()+" "+cacheTable.getRow("1").getFieldData("userName").getData());
//		System.out.println(cacheTable.getRow("2").getFieldData("id").getData()+" "+cacheTable.getRow("2").getFieldData("userName").getData());
		
		ArrayList<CacheTableRow> list = cacheTable.getRows();
		for(CacheTableRow row:list)
		{
			System.out.println(row.getFieldData("id")+" "+row.getFieldData("userName")+" "+row.getFieldData("number"));
		}
		
//		Comparator<CacheTableRow> sortOnComparator = new Comparator<CacheTableRow>(){
//			public int compare(CacheTableRow cacheTableRow1, CacheTableRow cacheTableRow2)
//			{
//				System.out.println("cacheTableRow1:"+cacheTableRow1 +" " + "cacheTableRow2:"+cacheTableRow2);
//				
//				int res=0;
//				Object fieldValue1=cacheTableRow1.getFieldData("id").getData();
//				Object fieldValue2=cacheTableRow2.getFieldData("id").getData();
//				
//				System.out.println("id1:"+fieldValue1);
//				System.out.println("id2:"+fieldValue2);
//				
//				if((int)fieldValue1<(int)fieldValue2)
//				{
//					res= -1;
//				}
//				else if((int)fieldValue1>(int)fieldValue2)
//				{
//					res= 1;
//				}
//				else
//				{
//					fieldValue1=cacheTableRow1.getFieldData("number").getData();
//					fieldValue2=cacheTableRow2.getFieldData("number").getData();
//					
//					System.out.println("number1:"+fieldValue1);
//					System.out.println("number2:"+fieldValue2);
//					
//					if((int)fieldValue1<(int)fieldValue2)
//					{
//						res= -1;
//					}
//					else if((int)fieldValue1>(int)fieldValue2)
//					{
//						res= 1;
//					}
//					else
//					{
//						res= 0;
//					}
//				}
//				System.out.println("res:"+res);
//				return res;
//			}
//		};
//		cacheTable.sort(sortOnComparator);
		cacheTable.sortOn(new Object[]{JavaType.INTEGER, "id", JavaType.INTEGER, "number"}, new SortType[]{SortType.ASC, SortType.ASC});
		System.out.println("--------------------------------");
		for(CacheTableRow row:list)
		{
			System.out.println(row.getFieldData("id")+" "+row.getFieldData("userName")+" "+row.getFieldData("number"));
		}
	}
}
